package com.idione.activities;

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.idione.model.DBAdapter;
import com.idione.model.QuestionSQLAdapter;
import com.idione.model.SignSQLAdapter;
import com.idione.util.LogUtil;
import com.idione.util.SoundSingleton;
import com.idione.util.VibSingleton;

public class Main extends Activity implements OnClickListener {

	private Button btnOption;  
	private Button btnPlay; 
	private Button btnCredit; 
	private Button btnSignal; 

	private ProgressDialog dialog;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		/* -----------------------------------------------------------------*
		 * vibration | sound												*
		 *------------------------------------------------------------------*/
		VibSingleton.getInstance().loadConfigVibrator();
		SoundSingleton.getInstance().loadConfigSound(this);

		/* -----------------------------------------------------------------*
		 * set Listener														*
		 *------------------------------------------------------------------*/
		btnOption  = (Button) findViewById(R.id.BtnOpcion);
		btnPlay = (Button) findViewById(R.id.BtnPlay);
		btnCredit = (Button) findViewById(R.id.BtnCredit);
		btnSignal = (Button) findViewById(R.id.BtnSignal);

		btnOption.setOnClickListener(this);
		btnPlay.setOnClickListener(this);
		btnCredit.setOnClickListener(this);
		btnSignal.setOnClickListener(this);

		this.setTypeFace();
		/* -----------------------------------------------------------------*
		 * Bundle State														*
		 *------------------------------------------------------------------*/
		if (savedInstanceState == null) {
			log("savedInstanceState == null");

			MediaPlayer back = MediaPlayer.create(this, R.raw.start);
			back.setVolume(0.2f, 0.2f);
			back.start();
			//SoundSingleton.getInstance().PlaySoundStart(); 

			/* -----------------------------------------------------------------*
			 * Data base														*
			 *------------------------------------------------------------------*/
			new loadBD().execute();
		} else {
			log("savedInstanceState != null");
		}
	}

	private void setTypeFace() {
		Typeface tfb = LogUtil.getTypeFaceButton(this);

		btnPlay.setTypeface(tfb);
		btnSignal.setTypeface(tfb);
		btnOption.setTypeface(tfb);
		btnCredit.setTypeface(tfb);
	}

	@Override
	public synchronized void onClick(View call) {
		SoundSingleton.getInstance().PlaySoundClick();
		switch (call.getId()) {
		case R.id.BtnOpcion:
			startActivity(new Intent(this, Configuration.class));
			overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out);
			break;
		case R.id.BtnPlay:
			startActivity(new Intent(this, Play.class));
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			break;
		case R.id.BtnCredit:
			startActivity(new Intent(this, Credit.class));
			overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
			break;
		case R.id.BtnSignal:
			startActivity(new Intent(this, Signal.class));
			overridePendingTransition(R.anim.push_rigth_in, R.anim.push_rigth_out);
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		SoundSingleton.getInstance().cleanup();
	}

	private class loadBD extends AsyncTask<Void, Void, Void> {
		
		@Override
		protected void onPreExecute() {
			dialog = new ProgressDialog(Main.this);
			dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			dialog.setMessage(getString(R.string.LblMsgBegin));
			dialog.setIndeterminate(true);
			dialog.setCancelable(false);
			dialog.setCanceledOnTouchOutside(false);
			dialog.show();
		}
		
		@Override
		protected Void doInBackground(Void... voi) {
			DBAdapter db = new DBAdapter(getBaseContext());

			db.open();
			try {
				new QuestionSQLAdapter(getBaseContext()).init(db.getDB());
				new SignSQLAdapter(getBaseContext()).init(db.getDB());
			} catch (IOException e) {
				e.printStackTrace();
			}
			db.close();
			return null;
		}
		
		@Override
		protected void onPostExecute(Void v) {
			if(dialog.isShowing())
				dialog.dismiss();
			Toast.makeText(Main.this, R.string.LblMsgEnd, Toast.LENGTH_SHORT).show();
		}
	}

	/*--------------------------------------------------------------------------*
	 * Log																		*
	 *--------------------------------------------------------------------------*/
	private final String LOGTAG = "LogsClass " + this.getClass().getName();
	private void log(String method) {
		if (LogUtil.activitiesLog)
			Log.i(LOGTAG, "... method : " + this.getClass().getSimpleName() + "." + method);
	}


}
